//  Inside Microsoft Exchange 2007 Web Services 
//  Chapter 10
//
//  Copyright (c) 2007 David Sterling, Ben Spain, Mark Taylor, Huw Upshall, Michael Mainer.  
//  All rights reserved.
//
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using ProxyHelpers.EWS;

namespace RespondToMeetingRequest
{
    class Program
    {
        static void Main(string[] args)
        {
            ExchangeServiceBinding binding = new ExchangeServiceBinding();
            binding.Url = @"https://yourServer/ews/Exchange.asmx";
            binding.Credentials = new NetworkCredential(
                "username",
                "password",
                "domain");

            // TODO: You must have the ItemId of a meeting request or calendar item in order to call
            // ResponseToMeetingRequest
            //
        }

        /// <summary>
        /// Registers a response to a meeting request, allows the caller to 
        /// request that read and/or delivery receipts be sent. (Listing 10-25)
        /// </summary>
        /// <remarks>
        /// This method will return an id for a new calendar item if
        ///     1. The meetingMessageOrCalendarItemId represents a calendar item.
        ///     2. responseToRegister is not set to Decline
        /// </remarks>
        /// <param name="binding">Binding to use for the web service call</param>
        /// <param name="meetingMessageOrCalendarItemId">Id of the meeting request 
        /// or meeting calendar item to register a response for</param>
        /// <param name="responseToRegister">type of response to register</param>
        /// <param name="meetingResponseBody">Optional message body to include</param>
        /// <param name="requestDeliveryReceipt">Set to true to request a delivery
        /// receipt be sent when the organizer receives the meeting response</param>
        /// <param name="requestReadReceipt">Set to true to reqest a read receipt
        /// be sent when the organizer reads the meeting response</param>
        /// <returns>Id of the newly created calendar item if possible 
        /// (see remarks)</returns>
        public static ItemIdType RespondToMeetingRequest(
            ExchangeServiceBinding binding,
            ItemIdType meetingMessageOrCalendarItemId,
            ResponseTypeType responseToRegister,
            string meetingResponseBody,
            bool requestDeliveryReceipt,
            bool requestReadReceipt)
        {
            // Determine the type of response object to create
            WellKnownResponseObjectType meetingResponseObject;
            switch (responseToRegister)
            {
                case ResponseTypeType.Accept:
                    meetingResponseObject = new AcceptItemType();
                    break;
                case ResponseTypeType.Tentative:
                    meetingResponseObject = new TentativelyAcceptItemType();
                    break;
                case ResponseTypeType.Decline:
                    meetingResponseObject = new DeclineItemType();
                    break;
                default:
                    throw new ArgumentException("ResponseTypeType value of "
                        + responseToRegister + " is not a valid meeting " +
                        "registration response.");
            }

            meetingResponseObject.ReferenceItemId = meetingMessageOrCalendarItemId;
            meetingResponseObject.IsDeliveryReceiptRequested =
                requestDeliveryReceipt;
            meetingResponseObject.IsDeliveryReceiptRequestedSpecified = true;
            meetingResponseObject.IsReadReceiptRequested =
                requestReadReceipt;
            meetingResponseObject.IsReadReceiptRequestedSpecified = true;

            // Check and set meeting response body content
            //
            if (!String.IsNullOrEmpty(meetingResponseBody))
            {
                meetingResponseObject.Body = new BodyType();
                meetingResponseObject.Body.BodyType1 = BodyTypeType.Text;
                meetingResponseObject.Body.Value = meetingResponseBody;
            }

            // Assemble our CreateItem request
            //
            CreateItemType createItemRequest = new CreateItemType();
            createItemRequest.MessageDisposition = MessageDispositionType.SendOnly;
            createItemRequest.MessageDispositionSpecified = true;
            createItemRequest.Items = new NonEmptyArrayOfAllItemsType();
            createItemRequest.Items.Items =
                new WellKnownResponseObjectType[] { meetingResponseObject };

            // Call the CreateItem web method and check for success.
            // If successful, see if there is an id that can be returned
            // and do so.
            //
            CreateItemResponseType response = binding.CreateItem(createItemRequest);
            ItemInfoResponseMessageType responseMessage =
                response.ResponseMessages.Items[0] as ItemInfoResponseMessageType;

            if (responseMessage.ResponseCode != ResponseCodeType.NoError)
            {
                throw new Exception(String.Format(
                    "Unable to register for meeting\r\n{0}\r\n{1}",
                    responseMessage.ResponseCode,
                    responseMessage.MessageText));
            }

            // If the id specified was a meeting request, then the Items array 
            // in the ItemInfoResponseMesssageType Items array will be 
            // null
            //
            if (responseMessage.Items.Items != null)
            {
                // There is an id of a calendar item that can be returned
                return responseMessage.Items.Items[0].ItemId;
            }
            else
            {
                // There is no id to be returned
                return null;
            }
        }

    }
}
